package view.editor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import view.Displayable;
import view.View;

import controller.editor.RelationCreationController;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import main.model.Attribute;
import main.model.Entity;
import main.model.Relation;
import javax.swing.JComboBox;

public class RelationCreationView extends View<RelationCreationController> implements Displayable {

	private JFrame frame;
	private JTextField textField;
	private RelationCreationController controller;
	private DefaultTableModel entitiesTableModel;
	private JTable entitiesTable;
	private DefaultTableModel attributesToRelation;
	private JTable tablaAtributos;
	private Relation relation;
	@SuppressWarnings("rawtypes")
	private JComboBox comboBox;

	public RelationCreationView() {
		setAttributesToRelation(new DefaultTableModel(){
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				if (column == 0 || column == 1){
					return false;
				}
				return true;
			}
		});
		getAttributesToRelation().addColumn ("Attributo");
		getAttributesToRelation().addColumn ("Cardinalidad");
		getAttributesToRelation().addColumn("Es Id?");
		getAttributesToRelation().addColumn("Editar");
		getAttributesToRelation().addColumn("Borrar");
		setEntitiesTableModel(new DefaultTableModel(){
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				if (column == 0){
					return false;
				}
				return true;
			}
		});
		entitiesTableModel.addColumn ("Entidad");
		entitiesTableModel.addColumn ("Cardinalidad");
		entitiesTableModel.addColumn("Borrar");
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initialize() {
		frame = new JFrame("Crear relacion");
		frame.setBounds(100, 100, 607, 515);
		frame.getContentPane().setLayout(null);

		JLabel lblNombre = new JLabel("Nombre");
		lblNombre.setBounds(43, 40, 114, 15);
		frame.getContentPane().add(lblNombre);

		textField = new JTextField();
		textField.setBounds(230, 38, 169, 19);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		setTablaAtributos(new JTable(getAttributesToRelation()));
		setEntitiesTable(new JTable(getEntitiesTableModel()));
		JButton btnEntidades = new JButton("Agregar entidad");
		btnEntidades.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getController().handleButtonAddEntity();
			}
		});
		btnEntidades.setBounds(229, 128, 169, 25);
		frame.getContentPane().add(btnEntidades);

		JLabel lblEntidad = new JLabel("Entidad");
		lblEntidad.setBounds(43, 133, 114, 15);
		frame.getContentPane().add(lblEntidad);

		JLabel lblAtributosComunes = new JLabel("Atributos comunes");
		lblAtributosComunes.setBounds(55, 267, 264, 19);
		frame.getContentPane().add(lblAtributosComunes);

		JButton btnAgregar_1 = new JButton("Agregar");
		btnAgregar_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getController().handleButtonAddAttributes();
			}
		});
		btnAgregar_1.setBounds(350, 264, 117, 25);
		frame.getContentPane().add(btnAgregar_1);

		JButton btnTerminar = new JButton("Terminar");
		btnTerminar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				getController().handleButtonTerminar();
			}
		});
		btnTerminar.setBounds(202, 448, 117, 25);
		frame.getContentPane().add(btnTerminar);

		JScrollPane scrollPane = new JScrollPane(getTablaAtributos());
		scrollPane.setBounds(43, 301, 462, 104);
		frame.getContentPane().add(scrollPane);

		JScrollPane scrollPane_1 = new JScrollPane(getEntitiesTable());
		scrollPane_1.setBounds(43, 175, 462, 77);
		frame.getContentPane().add(scrollPane_1);
		
		JLabel lblTipo = new JLabel("Tipo");
		lblTipo.setBounds(43, 82, 70, 15);
		frame.getContentPane().add(lblTipo);
		
		comboBox = new JComboBox();
		comboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {"Asociacion", "Composicion" }));
		comboBox.setBounds(230, 77, 170, 25);
		frame.getContentPane().add(comboBox);
	}


	public void fillRelationData() {
		if (this.getRelation() != null){
			this.textField.setText(this.getRelation().getName());
			if (this.getRelation().isComposition()){
				comboBox.setSelectedIndex(1);
			} else {
				comboBox.setSelectedIndex(0);
			}
			addEntitiesToRelation(this.getRelation().getEntities());
			addAttributesToTable(this.getRelation().getAttributes());
			addAttributesToTable(this.getRelation().getIdAttributes());
		}
	}
	
	public String getTypeSelected(){
		return this.comboBox.getSelectedItem().toString();
	}
	
	private void addEntitiesToRelation(List<Entity> entities) {
		ArrayList<String> l = new ArrayList<String>();
		for (Entity entity : entities) {
			l.add(entity.getName());
		}
		controller.addEntityToRelation(l);
	}

	private void addAttributesToTable(List<Attribute> attributes) {
		for (Attribute attribute : attributes) {
			controller.addAttributeToComponent(attribute);
		}
	}

	@Override
	public void show() {
		frame.setVisible(true);
	}

	@Override
	public void hide() {
		frame.setVisible(false);
	}

	@Override
	public RelationCreationController getController() {
		return this.controller;
	}

	@Override
	public void setController(RelationCreationController controller) {
		this.controller = controller;
	}

	public String getRelationName(){
		return this.textField.getText();
	}

	public DefaultTableModel getAttributesToRelation() {
		return attributesToRelation;
	}

	public void setAttributesToRelation(DefaultTableModel attributesToEntity) {
		this.attributesToRelation = attributesToEntity;
	}

	public DefaultTableModel getEntitiesTableModel() {
		return entitiesTableModel;
	}

	public void setEntitiesTableModel(DefaultTableModel entitiesTableModel) {
		this.entitiesTableModel = entitiesTableModel;
	}

	public JTable getTablaAtributos() {
		return tablaAtributos;
	}

	public void setTablaAtributos(JTable tablaAtributos) {
		this.tablaAtributos = tablaAtributos;
	}

	public JTable getEntitiesTable() {
		return entitiesTable;
	}

	public void setEntitiesTable(JTable entitiesTable) {
		this.entitiesTable = entitiesTable;
	}

	public void setRelation(Relation relation) {
		this.relation = relation;
	}

	public Relation getRelation() {
		return relation;
	}
}
